package top.serms.leetcode;

/**
 * @Author: SerMs
 * @Date: 2024/07/03 09 34
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 **/
public class LeetCode3099 {
    public static void main(String[] args) {
        int arr[] = {18, 29, 30, 10, 80};
        for (int i : arr) {
            System.out.println(sumOfTheDigitsOfHarshadNumber(i));
        }
    }

    public static int sumOfTheDigitsOfHarshadNumber(int x) {
       /* int s = 0;
        for (int y = x; y != 0; y /= 10) {
            s += (y % 10);
        }
        return x % s == 0 ? s : -1;*/

        int sum = 0, newX = x;
        while (newX > 0) {
            sum += newX % 10;
            newX /= 10;
        }
        return x % sum == 0 ? sum : -1;
    }
}
